/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fwdisp.gui.tabs.settings;

import fwdisp.gui.moduls.IDTable;
import fwdisp.gui.moduls.SecRowPanel;
import settings.MainSettingsManager;
import settings.collections.CarCollection;
import settings.collections.elements.Car;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author rbs
 */
public class CarSettingsPanel extends SecRowPanel{
    
    private static DefaultTableModel dtm;
    
    public CarSettingsPanel() {
        setName("Fahrzeuge");
        setVisible(true);
        setLayout(new BorderLayout(40, 40));

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        MainSettingsManager mainSettings = MainSettingsManager.getSetting();
        CarCollection carCollection = mainSettings.getCarCollection();
        
        String names[] = new String[]{"ID", "Fahrzeugname", "Funk-Kennung", "RIC"};

        dtm = new DefaultTableModel(names, 0);

        final IDTable table = new IDTable(dtm);
        dtm.addTableModelListener(new TableModelListener() {

            public void tableChanged(TableModelEvent tme) {
                
                int row = tme.getFirstRow();
                if (tme.getType() == TableModelEvent.UPDATE)
                {
                    System.out.println("Row edited: " + row
                                    + " Col edited: " + tme.getColumn());
                    MainSettingsManager setting = MainSettingsManager.getSetting();
                    CarCollection carCollection = setting.getCarCollection();
                    Car old_car = carCollection.getCar(tme.getFirstRow());
                    Car new_car = new Car(row, (String) dtm.getValueAt(row, 1), (String) dtm.getValueAt(row, 2), (String) dtm.getValueAt(row, 3));
                    carCollection.changeCar(old_car, new_car);
                    AusrueckSettingsPanel.updateCars();
                }
            }
        });
        
        //adding cars from file:
        ArrayList<Car> cars = carCollection.getCars();
        for (Car car: cars)
        {
            String[] row = {"" + car.getId(), car.getName(), car.getCallName(), car.getRic()};
            dtm.addRow(row); 
        }
        //AusrueckSettingsPanel.updateCars();
        
        
        
        add(new JScrollPane(table), BorderLayout.CENTER);

        JPanel new_object = new JPanel(new GridLayout(1, 3, 20, 0));
        
        JButton add = new JButton("+");
        add.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                MainSettingsManager setting = MainSettingsManager.getSetting();
                CarCollection carCollection = setting.getCarCollection();
                carCollection.addCar(new Car(dtm.getRowCount(), "", "", ""));
                dtm.addRow(new String[]{"" + (dtm.getRowCount()), "", "", ""});
                AusrueckSettingsPanel.updateCars();
            }
        });
        
        JButton sub = new JButton("-");
        sub.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                
                int selRow = table.getSelectedRow();
                if (selRow == -1)
                    if (table.getRowCount() > 0)
                        selRow = table.getRowCount() - 1;
                    else
                        return;
                
                MainSettingsManager setting = MainSettingsManager.getSetting();
                CarCollection carCollection = setting.getCarCollection();
                
                for (int i = selRow + 1; i < table.getRowCount(); i++)
                {
                    dtm.setValueAt(i - 1, i, 0);
                }
                System.out.println("Deleting Car: " + selRow);
                //DEBUG
                carCollection.delCar(selRow);
                carCollection.listRemainingCars();
                dtm.removeRow(selRow);
                AusrueckSettingsPanel.updateCars();
            }
        });
        
        new_object.add(sub);
        new_object.add(add);
        new_object.setBackground(getBackground());
        add(new_object, BorderLayout.SOUTH);
    }

    public static ArrayList<String> getCar_names() {
        ArrayList<String> car_names = new ArrayList<String>();
        for (int i=0; i < dtm.getRowCount(); i++)
            car_names.add((String) dtm.getValueAt(i, 1));
        
        return car_names;
    }
    
    
    


}
